@extends("admin.frame_layout")
<?php $top_selected = "信息管理";$page_menu = "会员注册协议" ?>
@section('content')
    <div class="formbody">
        <div class="formtitle"><span>会员注册协议编辑</span></div>

        <form action="{{route('admin.protocol.save',['id'=>isset($protocol)?($protocol->id):(0)])}}"
              method="post" id="protocol">
            {!! csrf_field() !!}

            <ul class="forminfo">

                {!! inputText("协议标题","title","text",
                    isset($protocol)?$protocol->title:"",
                    "标题建议最长不超过50个字符！") !!}

                <li>
                    <label class="lable">协议内容</label>
                    {{-- 加载编辑器的容器 --}}
                    <div style="float: left;width: 800px;">
                        <script name="content" id="content"
                                type="text/plain">{!! $protocol->content or "" !!}</script>
                    </div>
                    <div style="clear: both;"></div>
                </li>
                <li><label class="lable">&nbsp;</label>
                    <input type="submit" class="btn" value="确认保存"/>
                </li>
            </ul>
        </form>
    </div>
@endsection

@section('footer')
    <script src="{{asset('admin/js/ueditor/ueditor.config.js')}}"></script>
    <script src="{{asset('admin/js/ueditor/ueditor.all.js')}}"></script>
    {{-- 实例化编辑器 --}}
    <script type="text/javascript">
        $(function () {
            //初始化编辑器
            var ue = UE.getEditor('content', {
                initialFrameHeight: 250
            });

            $("#protocol").validate({
                rules: {
                    title: {
                        required: true,
                        maxlength: 100
                    },
                    content: {required: true}
                },
                errorPlacement: function (error, element) {
                    $(element).closest("form").find("i[for='" + element.attr("id") + "']").html(error);
                },
                submitHandler: function (form) {
                    if (ue.hasContents()) {
                        ajaxSub(form);
                    } else {
                        toastr["error"]("请编写协议内容！");
                    }
                }
            });

            /**
             * ajax提交请求
             */
            function ajaxSub(form) {
                $(form).ajaxSubmit({
                    dataType: "json",
                    type: "post",
                    success: function showSuccess(data) {
                        if (data.code == 0) {
                            toastr["success"](data.msg);
                            setInterval(function () {
                                window.location = "{{route('admin.protocol')}}";
                            }, 2000);
                        } else {
                            toastr["error"](data.msg);
                        }
                    }
                });
            }
        });
    </script>
@endsection
